﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;
using System.Data;

public partial class GaleryZoom : System.Web.UI.Page
{
    string[] PictureList = new string[0]; //[fotoFile.Length];
   public string pictureMap = ""; // Ar Databind šito variable var izmantot .ASPX lapā
   internal class CustomCompare : IComparer<string>
   {
       public int Compare(string x, string y)
       {
           return DateTime.Compare(
             Directory.GetCreationTime(y), Directory.GetCreationTime(x));
       }
   }
    

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ItemsGet();
        }
    }

    private void ItemsGet()
    {
        string folderPath = Request.QueryString["path"];
        string imageName = Request.QueryString["imageName"];

        if (!string.IsNullOrEmpty(folderPath))
        {
            string VirtualPath = Server.MapPath(folderPath);

            List<string> images = LoadPictures(VirtualPath); //all image names

            int currentIndex = images.IndexOf(imageName);        //currentPhoto

            lblCurrentPage.Text = "Bilde: " + (currentIndex+1).ToString() + " no " + images.Count; //label number of pics
            LinkButton2.PostBackUrl = "~/Gallery.aspx?folderPath=" + folderPath; //Back to image Gallery

            ViewState.Add("CurrentItem", currentIndex); //retain current item
            ViewState.Add("Images", images); //retain images list
            ViewState.Add("folderPath",folderPath); //retain folder path

            ImageUI.Attributes["src"] = folderPath.Replace("~/","") + "/" + images[currentIndex];
            HyperLinkSave.Attributes["href"] = folderPath.Replace("~/", "") + "/" + images[currentIndex];
            
        }

    }
    
    protected void cmdPrev_Click(object sender, ImageClickEventArgs e)
    {
        if (ViewState["Images"] != null && ViewState["CurrentItem"] != null)
        {
            int currentIndex = Convert.ToInt32(ViewState["CurrentItem"]);
            List<String> images = (List<string>)ViewState["Images"];
            string folderPath = (string)ViewState["folderPath"];


            if (currentIndex >= 1) currentIndex = currentIndex - 1;

            UpdateUI(currentIndex, images, folderPath);
        }

    }
    protected void cmdNext_Click1(object sender, ImageClickEventArgs e)
    {
        if (ViewState["Images"] != null && ViewState["CurrentItem"] != null)
        {
            int currentIndex = Convert.ToInt32(ViewState["CurrentItem"]);
            List<String> images = (List<string>)ViewState["Images"];
            string folderPath = (string)ViewState["folderPath"];

            if (currentIndex < images.Count-1) currentIndex = currentIndex + 1;

            UpdateUI(currentIndex, images, folderPath);
           
        }
    }

    private List<string> LoadPictures(string VirtualPath)
    {
        List<string> picNames = new List<string>();
        DirectoryInfo di = new DirectoryInfo(VirtualPath);

        foreach (FileInfo fileInfo in di.GetFiles())
        {
            if (!fileInfo.Name.StartsWith("Small"))
            picNames.Add(fileInfo.Name);            
        }

        return picNames;
       
    }

    private void UpdateUI(int currentIndex, List<String> images, string folderPath)
    {
        ViewState["CurrentItem"] = currentIndex;
        lblCurrentPage.Text = "Bilde: " + (currentIndex+1).ToString() + " no " + images.Count.ToString(); //label number of pics
        ImageUI.Attributes["src"] = folderPath.Replace("~/", "") + "/" + images[currentIndex];
        HyperLinkSave.Attributes["href"] = folderPath.Replace("~/", "") + "/" + images[currentIndex];
    }
}